package com.edooteach.api.web.controller.shopping;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.demo2do.core.entity.result.HttpResult;
import com.edoomath.entity.mall.Sku;
import com.edoomath.entity.trade.SalesOrder;
import com.edoomath.entity.user.User;
import com.edoomath.entity.user.UserAddress;
import com.edooteach.api.servcice.DataService;
import com.edooteach.api.servcice.SalesOrderService;
import com.edooteach.api.servcice.UserAddressApiService;
import com.edooteach.api.servcice.UserService;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/orders")
public class SalesOrderApiController {
    
    @Autowired
    private SalesOrderService salesOrderService;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private UserAddressApiService userAddressApiService;
    
    @Autowired
    private DataService<Sku> dataService;
    
    // create by user, address
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult onCreate(@RequestParam("skuId") Long skuId,
                               @RequestParam("unionid") String unionid,
                               @RequestParam("courseIds") List<Long> courseIds,
                               @RequestParam("addressId") Long addressId,
                               HttpServletRequest request) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            
            if(user == null || user.getMobile().isEmpty()) {
                return httpResult.fail("400").message("用户信息异常");
            }
            
            UserAddress userAddress = userAddressApiService.load(UserAddress.class, addressId);
            if(userAddress == null) {
                return httpResult.fail("400").message("用户地址信息异常");
            }
            
            Sku sku = dataService.load(Sku.class, skuId);
            if(sku == null) {
                return httpResult.fail("400").message("商品信息异常");
            }
            
            SalesOrder salesOrder = salesOrderService.create(user, userAddress, sku, courseIds);
            return httpResult.success().data("order", salesOrder).message("订单创建成功");
            
        } catch (Exception e) {
            return httpResult.fail("500").message("创建订单异常");
        }
    }
    
    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult list(@RequestParam("unionid") String unionid) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            if(user == null) {
                return httpResult.fail("500").message("获取订单异常");
            }
            List<SalesOrder> salesOrders = salesOrderService.list(user);
            Set<Long> skuIds=new HashSet<Long>();
            for(SalesOrder salesOrder : salesOrders) {
                skuIds.addAll(salesOrder.getSkus());
            }
            
            return httpResult.success().data("salesOrders", salesOrders)
                                       .data("skuIds", skuIds);
            
        } catch (Exception e) {
            return httpResult.fail("500").message("获取订单异常");
        }
        
    }
    
    // load by code
    /**
     * 
     * @param code
     * @return
     */
    @RequestMapping(value = "/{code}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult load(@PathVariable("code") String code) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            SalesOrder salesOrder = salesOrderService.load(code);
            httpResult.success().data("order", salesOrder);
            
            return httpResult;
            
        } catch (Exception e) {
            return httpResult.fail("500").message("获取订单异常");
        }
    }
}
